package cn.org.wifit.userserver.boot;

import cn.org.wifit.core.zookeeper.manager.ZookeeperReadManager;
import cn.org.wifit.core.zookeeper.manager.ZookeeperServerBootWriteManager;

/**
 * 主要的启动类，在这里主要进行spring的初始化、netty的初始化、zookeeper的初始化
 * @author yinwenjie
 */
public class BootStartup {
	
	private ZookeeperReadManager zookeeperReadManager;
	
	private ZookeeperServerBootWriteManager bootWriteManager;
	
	/**
	 * 启动管理方法
	 */
	public void startManager() throws Exception {
		/*
		 * 1、在zookeeperReadManager创建时，就会链接到zk上面
		 * ==========向zk boot节点注册启动请求，开启启动流程
		 * 2、链接上zk后，根据远程zk集群的信息，组件本地缓存
		 * 3、让本IMUser与远程各个已经启动的IMServer建立稳定的TCP链接，并将回话存储在IMUserTCPChannelQueen中
		 * 4、激活ZookeeperIMServerWriteManager，在zk上面建立描述自身的节点信息
		 * ==========向zk boot节点注销启动请求，完成启动流程
		 * 5、启动结束
		 * 
		 * 注意由于BootStartup不是主线程，所以不需要阻塞这个线程
		 * */
		this.syncStartupBootServer();
	}
	
	/**
	 * 向远程zookeeper集群注册分布式事务锁，以便保证多个服务器启动时的服务器顺序依次启动
	 * @param springContext
	 */
	private void syncStartupBootServer() {
		this.bootWriteManager.syncBoot();
	}

	public ZookeeperReadManager getZookeeperReadManager() {
		return zookeeperReadManager;
	}

	public void setZookeeperReadManager(ZookeeperReadManager zookeeperReadManager) {
		this.zookeeperReadManager = zookeeperReadManager;
	}

	public ZookeeperServerBootWriteManager getBootWriteManager() {
		return bootWriteManager;
	}

	public void setBootWriteManager(ZookeeperServerBootWriteManager bootWriteManager) {
		this.bootWriteManager = bootWriteManager;
	}
}
